package day12.console;

import day12.console.impl.*;
import day12.util.IDUtil;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author chenshijie
 * @date 2023/12/31 16:53
 */

public class ConsoleCommandManager implements ConsoleCommand {

    private final Map<Integer, ConsoleCommand> consoleCommandMap;

    public ConsoleCommandManager() {
        consoleCommandMap = new HashMap<>();
        consoleCommandMap.put(1, new SendToUserConsoleCommand());
        consoleCommandMap.put(2, new LogoutConsoleCommand());
        consoleCommandMap.put(3, new CreateGroupConsoleCommand());
        consoleCommandMap.put(4, new JoinGroupConsoleCommand());
        consoleCommandMap.put(5, new QuitGroupConsoleCommand());
        consoleCommandMap.put(6, new ListGroupMembersConsoleCommand());
        consoleCommandMap.put(7, new GroupMessageConsoleCommand());
    }
    @Override
    public void exec(Scanner sc, Channel channel) {
        System.out.println("┌─────────────────────────┐");
        System.out.println("│输入下述的数字指令执行相关操作 │");
        System.out.println("├─────────────────────────┤");
        System.out.println("│1.  单聊指令               │");
        System.out.println("│2.  退出登录               │");
        System.out.println("│3.  创建群聊               │");
        System.out.println("│4.  加入群聊               │");
        System.out.println("│5.  退出群聊               │");
        System.out.println("│6.  查看群员               │");
        System.out.println("│7.  群聊指令               │");
        System.out.println("└─────────────────────────┘");
        String command = sc.nextLine();
        if (!IDUtil.isNumeric(command)) {
            System.err.println("输入的指令不符合格式");
        } else {
            ConsoleCommand consoleCommand = consoleCommandMap.get(Integer.parseInt(command));
            if (consoleCommand != null) {
                consoleCommand.exec(sc, channel);
            } else {
                System.err.println("无法识别【" + command + "】指令，请重新输入！");
            }
        }

    }
}
